﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web.UI.WebControls;

namespace QuanLyVanBanBUS
{
    public class PhieuTrinhBUS
    {
        public IList<PhieuTrinh> layDanhSachPhieuTrinh()
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.PhieuTrinhs.OrderByDescending(q => q.MaPhieuTrinh).ToList();

            return query;
        }

        public void themPhieuTrinh(PhieuTrinh ptDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            storeDB.PhieuTrinhs.InsertOnSubmit(ptDTO);

            storeDB.SubmitChanges();
        }

        public void xoaPhieuTrinh(PhieuTrinh ptDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = (from pt in storeDB.PhieuTrinhs
                         where pt.MaPhieuTrinh == ptDTO.MaPhieuTrinh
                         select pt).First();

            storeDB.PhieuTrinhs.DeleteOnSubmit(query);

            storeDB.SubmitChanges();
        }

        public IList<PhieuTrinh> rptDanhSachPhieuTrinh(PhieuTrinh ptDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.PhieuTrinhs.Where(q => q.MaPhieuTrinh == ptDTO.MaPhieuTrinh).ToList();

            return query;
        }

        public void themItemVaoDropDownList(DropDownList dropDownList)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.PhieuTrinhs.ToList();
            foreach (var q in query)
            {
                var maPhieuTrinh = q.MaPhieuTrinh;
                var soVanBan = q.SoVanBan;

                ListItem item = new ListItem();
                item.Text = soVanBan;
                item.Value = maPhieuTrinh.ToString();

                dropDownList.Items.Add(item);
            }
        }

        public IList<PhieuTrinh> layThongTinPhieuTrinhTheoMaPhieuTrinh(PhieuTrinh ptDTO)
        {
            QLVBDataContext storeDB = new QLVBDataContext();

            var query = storeDB.PhieuTrinhs.Where(q => q.MaPhieuTrinh == ptDTO.MaPhieuTrinh).ToList();

            return query;
        }
    }
}
